package org.wei.tbk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

public class CVSHandle {
	
	/**
	 * taobao 商品导出cvs文件转换
	 * @param inputStream 文件 流
	 * @param map <自定义分类id：自定义分类中文名> 集合对，出现在map里面的就会被替换
	 * @return
	 */
	public static String covert(InputStream inputStream,HashMap<String,String> map) {
        try {
        	//1.读取cvs文件，淘宝的cvs是utf-16
        	InputStreamReader read = new InputStreamReader (inputStream,"UTF-16");
			CSVReader reader = new CSVReader(read,(char)0x09);
			
			//2.处理cvs，替换第2列的自定分类id===》自定分类名称
			String [] nextLine;
			 
//			Writer out = new StringWriter();
//			CSVWriter writer = new CSVWriter(out, (char)0x09);
//			
//			while ((nextLine = reader.readNext()) != null) {
//			    // nextLine[] is an array of values from the line
//				String s= nextLine[2];//",251974454,251974455,251979212,";
//		    	String[] dest = s.split(",");
//		    	if (dest!=null && dest.length>0) {
//					for (String in : dest) {
//						if (map.containsKey(in)) {
//							nextLine[2] = s.replace(in, map.get(in));
//						}
//					}
//				}
//				//3.写出新文件
//				 writer.writeNext(nextLine);
//			}
//			writer.flush();
//			return out.toString();		
			
			CSVWriter writer = new CSVWriter(new FileWriter("c://test.csv"),(char)0x09);
			while ((nextLine = reader.readNext()) != null) {
		    // nextLine[] is an array of values from the line
//			String s= nextLine[2];//",251974454,251974455,251979212,";
//	    	String[] dest = s.split(",");
//	    	if (dest!=null && dest.length>0) {
//				for (String in : dest) {
//					if (map.containsKey(in)) {
//						nextLine[2] = s.replace(in, map.get(in));
//					}
//				}
//			}
			//3.写出新文件
			 writer.writeNext(nextLine);
		}
		writer.flush();
			return null;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	
	
    public static void main(String[] args) throws FileNotFoundException {
    	
    	HashMap<String,String> map = new  HashMap<String,String>();
    	map.put("50010850", "类目1");
    	map.put("50010735", "类目2");
    	System.out.println(covert(new FileInputStream(new File("c:/101022.csv")),map));
    	
//    	//String s= ",251974454,251974455,251979212,";
//    	String s = "251974455";
//    	String[] dest = s.split(";");
//    	for(String in : dest){
//    		if(map.containsKey(in)){
//    			s = s.replace(in, map.get(in));
//    		}
//    	}
//    	System.out.println(s);
//    	System.out.println(Arrays.deepToString(dest));

System.out.print(new Character((char)0x22));
    } 
}
